let app = getApp()
import utils from "../../utils/util";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden: true,
        isStu: true,
        id: '',
        hostForm: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.initPage()
    },

    initPage() {
        utils.mask(this)
        // 获取用户 没有的话前往登录
        if (app.globalData.userType === -1) {
            utils.redirectToPageAndDistroy('/pages/login/login', this)
        } else {
            console.log(app.globalData.userType === 1)
            this.setData({
                isStu: (app.globalData.userType === 1),
                id: app.globalData.userInfo.id
            })
            this.loadHostInfo()
        }
    },

    loadHostInfo() {
        let url, that = this;
        if (this.data.isStu) {
            url = '/student/query'
        } else {
            url = '/shop/query'
        }
        wx.request({
            url: app.globalData.serviceUrl + url,
            data: {
                id: this.data.id
            },
            success: function (res) {
                let tempData = res.data
                if (tempData.success) {
                    app.globalData.userInfo = tempData.data
                    that.setData({
                        hostForm: tempData.data
                    })
                } else {
                    utils.toast('获取数据失败')
                }
                utils.removeMask(that)
            },
            fail: function () {
                utils.toast('获取数据失败')
                utils.removeMask(that)
            }
        })

    },
    uploadImg() {
        var that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res2) {
                var tempFilePaths = res2.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.serviceUrl + '/file/upload', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    headers: {
                        'Content-Type': 'form-data'
                    },
                    success: function (res) {
                        console.log(res.data)
                        let tempData = JSON.parse(res.data)
                        if (tempData.success) {
                            console.log(tempData.data)
                            that.setData({
                                ["hostForm.imgUrl"]: tempData.data,
                                ["hostForm.avatarUrl"]: tempData.data
                            });
                            utils.toast('上传成功')
                        } else {
                            utils.toast(tempData.msg)
                        }
                    },
                    fail: function () {
                        utils.toast('上传失败')
                    }
                })
            },
            fail: function () {
                utils.toast('请求失败')
            }
        });
    },
    logout() {
        app.globalData.userInfo = {}
        app.globalData.userType = -1
        utils.redirectToPageAndDistroy("/pages/login/login", this)
    },

    saveDetail() {
        let that = this
        let url = app.globalData.userType === 1 ? '/student/save' : '/shop/save'

        let data = this.data.hostForm
        delete data['updateTime']
        delete data['createTime']
        delete data['password']
        utils.mask(that)
        wx.request({
            url: app.globalData.serviceUrl + url,
            data: data,
            success: function (res) {
                let tempData = res.data
                if (tempData.success) {
                    utils.toast('保存成功')
                    setTimeout(function () {
                        wx.navigateBack({//返回
                            delta: 1
                        })
                    }, 1000)
                } else {
                    utils.toast(tempData.msg)
                }
                utils.removeMask(that)
            },
            fail: function () {
                utils.removeMask(that)
                utils.toast('请求失败')
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.initPage()
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /****************************input监听*************************/
    watchShopName(e) {
        this.setData({
            ["hostForm.shopName"]: e.detail.value
        })
    },

    watchName(e) {
        this.setData({
            ["hostForm.name"]: e.detail.value
        })
    },

    watchPhone(e) {
        this.setData({
            ["hostForm.phone"]: e.detail.value
        })
    },

    watchShool(e) {
        this.setData({
            ["hostForm.school"]: e.detail.value
        })
    },

    watchEmail(e) {
        this.setData({
            ["hostForm.email"]: e.detail.value
        })
    },

    watchshopAddress(e) {
        this.setData({
            ["hostForm.shopAddress"]: e.detail.value
        })
    },

    watchShopContent(e) {
        this.setData({
            ["hostForm.shopContent"]: e.detail.value
        })
    },

    watchNickName(e) {
        this.setData({
            ["hostForm.nickName"]: e.detail.value
        })
    },

    watchSex(e) {
        this.setData({
            ["hostForm.sex"]: e.detail.value
        })
    },

    watchAge(e) {
        this.setData({
            ["hostForm.age"]: e.detail.value
        })
    },

    watchClasses(e) {
        this.setData({
            ["hostForm.classes"]: e.detail.value
        })
    },

    watchAddress(e) {
        this.setData({
            ["hostForm.address"]: e.detail.value
        })
    },

    watchSchool(e) {
        this.setData({
            ["hostForm.school"]: e.detail.value
        })
    },

    watchMark(e) {
        this.setData({
            ["hostForm.mark"]: e.detail.value
        })
    },

    watchContent(e) {
        this.setData({
            ["hostForm.content"]: e.detail.value
        })
    },
})
